<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>验证码 | 欢迎学习django课程</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../part4/7.html" />
    
    
    <link rel="prev" href="../part4/5.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="4.5"
        data-chapter-title="验证码"
        data-filepath="part4/6.md"
        data-basepath=".."
        data-revision="Tue Jan 03 2017 08:43:08 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="part1/1.html">
            
                
                    <a href="../part1/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        入门
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="part1/2.html">
            
                
                    <a href="../part1/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        搭建开发环境
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="part1/3.html">
            
                
                    <a href="../part1/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        设计模型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="part1/4.html">
            
                
                    <a href="../part1/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        管理站点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="part1/5.html">
            
                
                    <a href="../part1/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="part1/6.html">
            
                
                    <a href="../part1/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        模板
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="part1/7.html">
            
                
                    <a href="../part1/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="part2/1.html">
            
                
                    <a href="../part2/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        模型
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="part2/2.html">
            
                
                    <a href="../part2/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        定义模型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="part2/3.html">
            
                
                    <a href="../part2/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        模型成员
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="part2/4.html">
            
                
                    <a href="../part2/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        模型查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="part2/5.html">
            
                
                    <a href="../part2/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        自连接
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="part2/6.html">
            
                
                    <a href="../part2/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="part3/1.html">
            
                
                    <a href="../part3/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        视图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="part3/2.html">
            
                
                    <a href="../part3/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        URLconf
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="part3/3.html">
            
                
                    <a href="../part3/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        视图函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="part3/4.html">
            
                
                    <a href="../part3/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        Request对象
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.3.1" data-path="part3/4_1.html">
            
                
                    <a href="../part3/4_1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.1.</b>
                        
                        QueryDict对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.2" data-path="part3/4_2.html">
            
                
                    <a href="../part3/4_2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.2.</b>
                        
                        GET属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.3" data-path="part3/4_3.html">
            
                
                    <a href="../part3/4_3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.3.</b>
                        
                        POST属性
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="part3/5.html">
            
                
                    <a href="../part3/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        Response对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="part3/6.html">
            
                
                    <a href="../part3/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        状态保持
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="part3/7.html">
            
                
                    <a href="../part3/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="part4/1.html">
            
                
                    <a href="../part4/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="part4/2.html">
            
                
                    <a href="../part4/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        定义模板
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="part4/3.html">
            
                
                    <a href="../part4/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        模板继承
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="part4/4.html">
            
                
                    <a href="../part4/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        HTML转义
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="part4/5.html">
            
                
                    <a href="../part4/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        CSRF
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="4.5" data-path="part4/6.html">
            
                
                    <a href="../part4/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        验证码
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="part4/7.html">
            
                
                    <a href="../part4/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="part5/1.html">
            
                
                    <a href="../part5/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="part5/2.html">
            
                
                    <a href="../part5/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        静态文件处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="part5/3.html">
            
                
                    <a href="../part5/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        中间件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="part5/4.html">
            
                
                    <a href="../part5/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        上传图片
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="part5/5.html">
            
                
                    <a href="../part5/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        Admin站点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="part5/6.html">
            
                
                    <a href="../part5/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        分页
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="part5/7.html">
            
                
                    <a href="../part5/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        ajax
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="part5/8.html">
            
                
                    <a href="../part5/8.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="part6/1.html">
            
                
                    <a href="../part6/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        第三方
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="part6/2.html">
            
                
                    <a href="../part6/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        富文本编辑器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="part6/3.html">
            
                
                    <a href="../part6/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        缓存
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="part6/4.html">
            
                
                    <a href="../part6/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        全文检索
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="part6/5.html">
            
                
                    <a href="../part6/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        celery
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="part6/6.html">
            
                
                    <a href="../part6/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        布署
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="part6/7.html">
            
                
                    <a href="../part6/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="part7/1.html">
            
                
                    <a href="../part7/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        Git与项目
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="part7/2.html">
            
                
                    <a href="../part7/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        Git简介
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1.1" data-path="part7/2_2.html">
            
                
                    <a href="../part7/2_2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.1.</b>
                        
                        远程仓库
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.1.2" data-path="part7/2_1.html">
            
                
                    <a href="../part7/2_1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.2.</b>
                        
                        本地仓库
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="part7/3.html">
            
                
                    <a href="../part7/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        项目设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="part7/4.html">
            
                
                    <a href="../part7/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        模型类设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="part7/5.html">
            
                
                    <a href="../part7/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        使用页面
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.5" data-path="part7/6.html">
            
                
                    <a href="../part7/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.5.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >欢迎学习django课程</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x9A8C;&#x8BC1;&#x7801;">&#x9A8C;&#x8BC1;&#x7801;</h1>
<ul>
<li>&#x5728;&#x7528;&#x6237;&#x6CE8;&#x518C;&#x3001;&#x767B;&#x5F55;&#x9875;&#x9762;&#xFF0C;&#x4E3A;&#x4E86;&#x9632;&#x6B62;&#x66B4;&#x529B;&#x8BF7;&#x6C42;&#xFF0C;&#x53EF;&#x4EE5;&#x52A0;&#x5165;&#x9A8C;&#x8BC1;&#x7801;&#x529F;&#x80FD;&#xFF0C;&#x5982;&#x679C;&#x9A8C;&#x8BC1;&#x7801;&#x9519;&#x8BEF;&#xFF0C;&#x5219;&#x4E0D;&#x9700;&#x8981;&#x7EE7;&#x7EED;&#x5904;&#x7406;&#xFF0C;&#x53EF;&#x4EE5;&#x51CF;&#x8F7B;&#x4E00;&#x4E9B;&#x670D;&#x52A1;&#x5668;&#x7684;&#x538B;&#x529B;</li>
<li>&#x4F7F;&#x7528;&#x9A8C;&#x8BC1;&#x7801;&#x4E5F;&#x662F;&#x4E00;&#x79CD;&#x6709;&#x6548;&#x7684;&#x9632;&#x6B62;crsf&#x7684;&#x65B9;&#x6CD5;</li>
<li>&#x9A8C;&#x8BC1;&#x7801;&#x6548;&#x679C;&#x5982;&#x4E0B;&#x56FE;&#xFF1A;</li>
</ul>
<p><img src="images/verifycode.png" alt="&#x9A8C;&#x8BC1;&#x7801;"></p>
<h4 id="&#x9A8C;&#x8BC1;&#x7801;&#x89C6;&#x56FE;">&#x9A8C;&#x8BC1;&#x7801;&#x89C6;&#x56FE;</h4>
<ul>
<li>&#x65B0;&#x5EFA;viewsUtil.py&#xFF0C;&#x5B9A;&#x4E49;&#x51FD;&#x6570;verifycode</li>
<li>&#x6B64;&#x6BB5;&#x4EE3;&#x7801;&#x7528;&#x5230;&#x4E86;PIL&#x4E2D;&#x7684;Image&#x3001;ImageDraw&#x3001;ImageFont&#x6A21;&#x5757;&#xFF0C;&#x9700;&#x8981;&#x5148;&#x5B89;&#x88C5;Pillow&#xFF08;3.4.1&#xFF09;&#x5305;&#xFF0C;&#x8BE6;&#x7EC6;&#x6587;&#x6863;&#x53C2;&#x8003;<a href="http://pillow.readthedocs.io/en/3.4.x/" target="_blank">http://pillow.readthedocs.io/en/3.4.x/</a></li>
<li>Image&#x8868;&#x793A;&#x753B;&#x5E03;&#x5BF9;&#x8C61;</li>
<li>ImageDraw&#x8868;&#x793A;&#x753B;&#x7B14;&#x5BF9;&#x8C61;</li>
<li><p>ImageFont&#x8868;&#x793A;&#x5B57;&#x4F53;&#x5BF9;&#x8C61;&#xFF0C;ubuntu&#x7684;&#x5B57;&#x4F53;&#x8DEF;&#x5F84;&#x4E3A;&#x201C;/usr/share/fonts/truetype/freefont&#x201D;</p>
</li>
<li><p>&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
</li>
</ul>
<pre><code>from django.http import HttpResponse
def verifycode(request):
    #&#x5F15;&#x5165;&#x7ED8;&#x56FE;&#x6A21;&#x5757;
    from PIL import Image, ImageDraw, ImageFont
    #&#x5F15;&#x5165;&#x968F;&#x673A;&#x51FD;&#x6570;&#x6A21;&#x5757;
    import random
    #&#x5B9A;&#x4E49;&#x53D8;&#x91CF;&#xFF0C;&#x7528;&#x4E8E;&#x753B;&#x9762;&#x7684;&#x80CC;&#x666F;&#x8272;&#x3001;&#x5BBD;&#x3001;&#x9AD8;
    bgcolor = (random.randrange(20, 100), random.randrange(
        20, 100), 255)
    width = 100
    height = 25
    #&#x521B;&#x5EFA;&#x753B;&#x9762;&#x5BF9;&#x8C61;
    im = Image.new(&apos;RGB&apos;, (width, height), bgcolor)
    #&#x521B;&#x5EFA;&#x753B;&#x7B14;&#x5BF9;&#x8C61;
    draw = ImageDraw.Draw(im)
    #&#x8C03;&#x7528;&#x753B;&#x7B14;&#x7684;point()&#x51FD;&#x6570;&#x7ED8;&#x5236;&#x566A;&#x70B9;
    for i in range(0, 100):
        xy = (random.randrange(0, width), random.randrange(0, height))
        fill = (random.randrange(0, 255), 255, random.randrange(0, 255))
        draw.point(xy, fill=fill)
    #&#x5B9A;&#x4E49;&#x9A8C;&#x8BC1;&#x7801;&#x7684;&#x5907;&#x9009;&#x503C;
    str1 = &apos;ABCD123EFGHIJK456LMNOPQRS789TUVWXYZ0&apos;
    #&#x968F;&#x673A;&#x9009;&#x53D6;4&#x4E2A;&#x503C;&#x4F5C;&#x4E3A;&#x9A8C;&#x8BC1;&#x7801;
    rand_str = &apos;&apos;
    for i in range(0, 4):
        rand_str += str1[random.randrange(0, len(str1))]
    #&#x6784;&#x9020;&#x5B57;&#x4F53;&#x5BF9;&#x8C61;
    font = ImageFont.truetype(&apos;FreeMono.ttf&apos;, 23)
    #&#x6784;&#x9020;&#x5B57;&#x4F53;&#x989C;&#x8272;
    fontcolor = (255, random.randrange(0, 255), random.randrange(0, 255))
    #&#x7ED8;&#x5236;4&#x4E2A;&#x5B57;
    draw.text((5, 2), rand_str[0], font=font, fill=fontcolor)
    draw.text((25, 2), rand_str[1], font=font, fill=fontcolor)
    draw.text((50, 2), rand_str[2], font=font, fill=fontcolor)
    draw.text((75, 2), rand_str[3], font=font, fill=fontcolor)
    #&#x91CA;&#x653E;&#x753B;&#x7B14;
    del draw
    #&#x5B58;&#x5165;session&#xFF0C;&#x7528;&#x4E8E;&#x505A;&#x8FDB;&#x4E00;&#x6B65;&#x9A8C;&#x8BC1;
    request.session[&apos;verifycode&apos;] = rand_str
    #&#x5185;&#x5B58;&#x6587;&#x4EF6;&#x64CD;&#x4F5C;
    import cStringIO
    buf = cStringIO.StringIO()
    #&#x5C06;&#x56FE;&#x7247;&#x4FDD;&#x5B58;&#x5728;&#x5185;&#x5B58;&#x4E2D;&#xFF0C;&#x6587;&#x4EF6;&#x7C7B;&#x578B;&#x4E3A;png
    im.save(buf, &apos;png&apos;)
    #&#x5C06;&#x5185;&#x5B58;&#x4E2D;&#x7684;&#x56FE;&#x7247;&#x6570;&#x636E;&#x8FD4;&#x56DE;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;&#xFF0C;MIME&#x7C7B;&#x578B;&#x4E3A;&#x56FE;&#x7247;png
    return HttpResponse(buf.getvalue(), &apos;image/png&apos;)
</code></pre><h4 id="&#x914D;&#x7F6E;url">&#x914D;&#x7F6E;url</h4>
<ul>
<li>&#x5728;urls.py&#x4E2D;&#x5B9A;&#x4E49;&#x8BF7;&#x6C42;&#x9A8C;&#x8BC1;&#x7801;&#x89C6;&#x56FE;&#x7684;url</li>
</ul>
<pre><code>from . import viewsUtil

urlpatterns = [
    url(r&apos;^verifycode/$&apos;, viewsUtil.verifycode),
]
</code></pre><h4 id="&#x663E;&#x793A;&#x9A8C;&#x8BC1;&#x7801;">&#x663E;&#x793A;&#x9A8C;&#x8BC1;&#x7801;</h4>
<ul>
<li>&#x5728;&#x6A21;&#x677F;&#x4E2D;&#x4F7F;&#x7528;img&#x6807;&#x7B7E;&#xFF0C;src&#x6307;&#x5411;&#x9A8C;&#x8BC1;&#x7801;&#x89C6;&#x56FE;</li>
</ul>
<pre><code>&lt;img id=&apos;verifycode&apos; src=&quot;/verifycode/&quot; alt=&quot;CheckCode&quot;/&gt;
</code></pre><ul>
<li>&#x542F;&#x52A8;&#x670D;&#x52A1;&#x5668;&#xFF0C;&#x67E5;&#x770B;&#x663E;&#x793A;&#x6210;&#x529F;</li>
<li>&#x6269;&#x5C55;&#xFF1A;&#x70B9;&#x51FB;&#x201C;&#x770B;&#x4E0D;&#x6E05;&#xFF0C;&#x6362;&#x4E00;&#x4E2A;&#x201D;&#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x6362;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x9A8C;&#x8BC1;&#x7801;</li>
</ul>
<pre><code>&lt;script type=&quot;text/javascript&quot; src=&quot;/static/jquery-1.12.4.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
    $(function(){
        $(&apos;#verifycodeChange&apos;).css(&apos;cursor&apos;,&apos;pointer&apos;).click(function() {
            $(&apos;#verifycode&apos;).attr(&apos;src&apos;,$(&apos;#verifycode&apos;).attr(&apos;src&apos;)+1)
        });
    });
&lt;/script&gt;
&lt;img id=&apos;verifycode&apos; src=&quot;/verifycode/?1&quot; alt=&quot;CheckCode&quot;/&gt;
&lt;span id=&apos;verifycodeChange&apos;&gt;&#x770B;&#x4E0D;&#x6E05;&#xFF0C;&#x6362;&#x4E00;&#x4E2A;&lt;/span&gt;
</code></pre><ul>
<li>&#x4E3A;&#x4E86;&#x80FD;&#x591F;&#x5B9E;&#x73B0;&#x63D0;&#x4EA4;&#x529F;&#x80FD;&#xFF0C;&#x9700;&#x8981;&#x589E;&#x52A0;form&#x548C;input&#x6807;&#x7B7E;</li>
</ul>
<pre><code>&lt;form method=&apos;post&apos; action=&apos;/verifycodeValid/&apos;&gt;
    &lt;input type=&quot;text&quot; name=&quot;vc&quot;&gt;
    &lt;img id=&apos;verifycode&apos; src=&quot;/verifycode/?1&quot; alt=&quot;CheckCode&quot;/&gt;
&lt;span id=&apos;verifycodeChange&apos;&gt;&#x770B;&#x4E0D;&#x6E05;&#xFF0C;&#x6362;&#x4E00;&#x4E2A;&lt;/span&gt;
&lt;br&gt;
&lt;input type=&quot;submit&quot; value=&quot;&#x63D0;&#x4EA4;&quot;&gt;
&lt;/form&gt;
</code></pre><h4 id="&#x9A8C;&#x8BC1;">&#x9A8C;&#x8BC1;</h4>
<ul>
<li>&#x63A5;&#x6536;&#x8BF7;&#x6C42;&#x7684;&#x4FE1;&#x606F;&#xFF0C;&#x4E0E;session&#x4E2D;&#x7684;&#x5185;&#x5BB9;&#x5BF9;&#x6BD4;</li>
</ul>
<pre><code>from django.http import HttpResponse

def verifycodeValid(request):
    vc = request.POST[&apos;vc&apos;]
    if vc.upper() == request.session[&apos;verifycode&apos;]:
        return HttpResponse(&apos;ok&apos;)
    else:
        return HttpResponse(&apos;no&apos;)
</code></pre><ul>
<li>&#x914D;&#x7F6E;&#x9A8C;&#x8BC1;&#x5904;&#x7406;&#x7684;url</li>
</ul>
<pre><code>urlpatterns = [
    url(r&apos;^verifycodeValid/$&apos;, views.verifycodeValid),
]
</code></pre><h4 id="&#x7B2C;&#x4E09;&#x65B9;">&#x7B2C;&#x4E09;&#x65B9;</h4>
<ul>
<li>&#x53EF;&#x4EE5;&#x5728;&#x7F51;&#x4E0A;&#x641C;&#x7D22;&#x201C;&#x9A8C;&#x8BC1;&#x7801;&#x201D;&#xFF0C;&#x627E;&#x5230;&#x4E00;&#x4E9B;&#x7B2C;&#x4E09;&#x65B9;&#x9A8C;&#x8BC1;&#x7801;&#x63D0;&#x4F9B;&#x7F51;&#x7AD9;&#xFF0C;&#x9605;&#x8BFB;&#x6587;&#x6863;&#xFF0C;&#x4F7F;&#x7528;&#x5230;&#x9879;&#x76EE;&#x4E2D;</li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../part4/5.html" class="navigation navigation-prev " aria-label="Previous page: CSRF"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../part4/7.html" class="navigation navigation-next " aria-label="Next page: 总结与作业"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
